class ComponentControllerTests extends ControllerTestCase {

    void tearDown() {
        def remove = GroovySystem.metaClassRegistry.&removeMetaClass
        remove Component
    }

    void testList() {
        List expected = [new Component()]
        Component.metaClass.'static'.list = {params -> return expected}
        withController(ComponentController) {controller ->
            def resultMap = controller.list()
            assertEquals('map should match ', [componentList: expected], resultMap)
        }
    }

    void testListWithMaxParams() {
        List expected = [new Component()]
        def MAX_PARAM = 2
        Component.metaClass.'static'.list = {params ->
            assert params.max == MAX_PARAM
            return expected
        }
        withController(ComponentController) {controller ->
            controller.params.max = MAX_PARAM
            def resultMap = controller.list()
            assertEquals('map should match ', [componentList: expected], resultMap)
        }
    }

    void testShow() {
        def ID = 1
        def expected = new Component()
        Component.metaClass.'static'.get = {id ->
            assert id == ID
            return expected
        }

        withController(ComponentController) {controller ->
            controller.params.id = ID
            def resultMap = controller.show()
            assertEquals('component should match expected', expected, resultMap.component)
        }
    }

    void testShowComponentNotFound() {
        def ID = 1
        Component.metaClass.'static'.get = {id ->
            assert id == ID
            return null
        }

        withController(ComponentController) {controller ->
            controller.params.id = ID
            controller.show()
            assertNotNull('flash message should be populated', controller.flash.message)
            assertEquals('action should match', controller.list, redirectMap.action)
        }
    }

}